package com.GroupSix.service;

import com.GroupSix.dao.ExamAnswerDao;
import com.GroupSix.entity.ExamAnswerEntity;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("studentScoreService")
@Slf4j
public class StudentScoreServiceImpl  implements StudentScoreService {

    @Autowired
    private ExamAnswerDao examAnswerDao;

    @Autowired
    private ExamPaperService examPaperService;

    @Autowired
    private ExamAnswerService examAnswerService;

    @Autowired
    private StudentService studentService;

    //统计学生详细成绩信息
    @Override
    public Map<String, Object> getStudentScoreDetail(int studentId, int examId) {
        Map<String, Object> result = new HashMap<>();

        // 查询考试答案记录
        LambdaQueryWrapper<ExamAnswerEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ExamAnswerEntity::getStudentId, studentId)
                .eq(ExamAnswerEntity::getExamId, examId);
        ExamAnswerEntity examAnswer = examAnswerDao.selectOne(queryWrapper);

        if (examAnswer != null) {

            // 基本信息
            result.put("studentId", examAnswer.getStudentId());
            result.put("examId", examAnswer.getExamId());
            result.put("score", examAnswer.getScore());
            result.put("startTime", examAnswer.getStartTime());
            result.put("submitTime", examAnswer.getSubmitTime());

            // 计算考试用时
            if (examAnswer.getStartTime() != null && examAnswer.getSubmitTime() != null) {
                Duration duration = Duration.between(examAnswer.getStartTime(), examAnswer.getSubmitTime());
                result.put("usedTime", duration.toMinutes());
            }


            // 难度分析
            String difficultyAnalysis = analyzeDifficulty(examAnswer.getScore());
            result.put("difficultyAnalysis", difficultyAnalysis);

        }
        return result;
    }

    private String analyzeDifficulty(Double score) {
        if (score >= 90) {
            return "该同学对该试卷所考察内容掌握较好";
        } else if (score >= 75) {
            return "该同学对该试卷所考察内容掌握一般";
        } else {
            return "该同学对该试卷所考察内容掌握较差";
        }
    }

    //展示全部成绩信息
    @Override
    public List<Map<String, Object>>show(){
        List<Map<String, Object>> ret = new ArrayList<>();
        List<ExamAnswerEntity> examAnswerList = examAnswerService.list();
        for (ExamAnswerEntity examAnswerEntity : examAnswerList) {
            Map <String, Object> paper = new HashMap<>();
            paper.put("studentId", examAnswerEntity.getStudentId());
            paper.put("studentName", studentService.getByStudentId(examAnswerEntity
                    .getStudentId()).getStudentName());//根据学号查找到学生姓名
            paper.put("examId", examAnswerEntity.getExamId());
            paper.put("subject", examPaperService.getById(examAnswerEntity.
                    getExamId()).getPaperName());//根据试卷id查找到试卷科目
            paper.put("score", examAnswerEntity.getScore());
            ret.add(paper);
        }
        return ret;
    }
}